package battle
{
	import data.SoldierInfo;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	
	import loaders.DisplayLoader;
	import loaders.ILoader;
	import loaders.LoaderManager;
	import loaders.LoaderQueue;
	
	import map.data.RadCell;

	public class Soldier extends MapItem
	{
		
		private var _info:SoldierInfo;
		private var _loaderQueue:LoaderQueue = new LoaderQueue();
		private var _views:Dictionary = new Dictionary(true);
		private var _currentCells:Vector.<RadCell>;
		
		public function Soldier(info:SoldierInfo)
		{
			_info = info;
		}
		
		override public function loadResource():void
		{
			var actions:Array = _info.actions.split('|');
			var loader:ILoader;
			for(var i:int = 0;i<actions.length;i++)
			{
				loader = LoaderManager.Instance.creatLoaderUrl('character/'+_info.avatar+'/'+actions[i]+'.swf',onItemComplete);
				loader.data = _info.avatar+'|'+actions[i];
				_loaderQueue.addLoader(loader);
			}
			if(hasWeapon())
			{
				var weaponRelatedAction:Array = ['stand','move','shoot'];
				for(var j:int = 0;j<actions.length;j++)
				{
					loader = LoaderManager.Instance.creatLoaderUrl('character/'+_info.weapon+'/'+weaponRelatedAction[j]+'.swf',onItemComplete);
					loader.data = _info.weapon+'|'+actions[j];
					_loaderQueue.addLoader(loader);
				}
			}
			_loaderQueue.start();
		}
		
		/**
		 * 
		 * @param distance 目标的距离
		 * @return 在没有障碍物遮挡的情况下对目标的伤害值
		 * 
		 */		
		public function getDamage(distance:Number):Number
		{
			if(distance>_info.range)return 0;
			return 0;
		}
		
		public function hasWeapon():Boolean
		{
			return _info.weapon != ''
		}
		
		private function onItemComplete(loader:DisplayLoader):void
		{
			_views[loader.data] = loader.content;
		}
		
		private var _currentAction:String = '';
		
		public function doAction(value:String):void
		{
			_currentAction = value;
		}
		
		public function get currentAction():String
		{
			return _currentAction;
		}
		
		private var _pos:Point
		
		public function set pos(value:Point):void
		{
			if(_pos.equals(value))return;
			_pos = value;
			view.x = _pos.x;
			view.y = _pos.y;
		}
		
		public function get pos():Point
		{
			return _pos;
		}
		
		public function get info():SoldierInfo
		{
			return _info;
		}
		
		override public function get view():DisplayObject
		{
			return _views[_info.avatar+'|'+_currentAction];
		}

		public function get currentCells():Vector.<RadCell>
		{
			return _currentCells;
		}

		public function set currentCells(value:Vector.<RadCell>):void
		{
			_currentCells = value;
		}
	}
}